/*
 * DILARANG MENGHAPUS ATAU MENGEDIT COPYRIGHT INI.
 * 
 * Copyright 2008 echo.khannedy@gmail.com. 
 * All rights reserved.
 * 
 * Semua isi dalam file ini adalah hak milik dari echo.khannedy@gmail.com
 * Anda tak diperkenankan untuk menggunakan file atau mengubah file
 * ini kecuali anda tidak menghapus atau merubah lisence ini.
 * 
 * File ini dibuat menggunakan :
 * IDE        : NetBeans
 * NoteBook   : Acer Aspire 5920G
 * OS         : Windows Vista
 * Java       : Java 1.6
 */
package usu.buku.telepon.admin;

import com.mysql.jdbc.Driver;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.swing.ImageIcon;
import usu.util.StringUtil;
import usu.widget.Form;

/**
 *
 * @author  usu
 */
public class AdminLogIn extends Form {

    private static final long serialVersionUID = -1;
    private StringBuilder string;
    private AdminForm form;

    /** Creates new form BeanForm
     * @throws java.io.IOException 
     */
    public AdminLogIn() throws IOException {
        
        setIconImage(new ImageIcon(getClass().getResource("/usu/buku/telepon/resource/small.png")).getImage());
        
        initVariables();
        initComponents();
    }

    /**
     * 
     * @return
     * @throws java.sql.SQLException
     */
    public Connection getConnection() throws SQLException {
        new Driver();
        return DriverManager.getConnection(string.toString(),
                textUsername.getText(), StringUtil.convertToString(textPassword.getPassword()));
    }

    private void initVariables() throws IOException {
        form = new AdminForm();

        Properties prop = new Properties();
        prop.loadFromXML(new FileInputStream("setting\\database.xml"));

        string = new StringBuilder();
        string.append("jdbc:mysql://");
        string.append(prop.getProperty("HOST"));
        string.append(":");
        string.append(prop.getProperty("PORT"));
        string.append("/");
        string.append(prop.getProperty("DATABASE"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        usu.widget.glass.ViewPortTransparan viewPortTransparan1 = new usu.widget.glass.ViewPortTransparan();
        textArea = new usu.buku.telepon.widget.TextArea();
        about = new usu.buku.telepon.widget.About();
        glassPane = new usu.widget.GlassPane();
        usu.buku.telepon.widget.Panel panel1 = new usu.buku.telepon.widget.Panel();
        usu.buku.telepon.widget.Label label1 = new usu.buku.telepon.widget.Label();
        usu.buku.telepon.widget.Label label2 = new usu.buku.telepon.widget.Label();
        usu.buku.telepon.widget.Label label3 = new usu.buku.telepon.widget.Label();
        usu.buku.telepon.widget.Label label4 = new usu.buku.telepon.widget.Label();
        textUsername = new usu.buku.telepon.widget.TextBox();
        textPassword = new usu.buku.telepon.widget.PasswordBox();
        usu.buku.telepon.widget.Button buttonMasuk = new usu.buku.telepon.widget.Button();
        usu.buku.telepon.widget.Button buttonReset = new usu.buku.telepon.widget.Button();
        usu.buku.telepon.widget.Button button1 = new usu.buku.telepon.widget.Button();
        usu.buku.telepon.widget.Label label5 = new usu.buku.telepon.widget.Label();
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();

        viewPortTransparan1.setView(textArea);

        textArea.setColumns(20);
        textArea.setEditable(false);
        textArea.setRows(5);

        about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutActionPerformed(evt);
            }
        });

        setGlassPane(glassPane);
        getGlassPane().setVisible(true);
        glassPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 4, 4));

        setTitle("e'PhoneBook");
        setAnimationHide(usu.widget.constan.Animation.HIDE_TO_TOP);
        setAnimationShow(usu.widget.constan.Animation.SHOW_FROM_TOP);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        label1.setText("e'PhoneBook");
        label1.setFont(new java.awt.Font("Tahoma", 1, 24));

        label2.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        label2.setIconReflec(new javax.swing.ImageIcon(getClass().getResource("/usu/buku/telepon/resource/mobile telephone.png"))); // NOI18N

        label3.setDisplayedMnemonic('U');
        label3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        label3.setText("username :");

        label4.setDisplayedMnemonic('P');
        label4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        label4.setText("password :");

        textUsername.setFocusAccelerator('U');
        textUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textUsernameActionPerformed(evt);
            }
        });

        textPassword.setFocusAccelerator('P');
        textPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textPasswordActionPerformed(evt);
            }
        });

        buttonMasuk.setMnemonic('M');
        buttonMasuk.setText("masuk");
        buttonMasuk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonMasukActionPerformed(evt);
            }
        });

        buttonReset.setMnemonic('R');
        buttonReset.setText("reset");
        buttonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonResetActionPerformed(evt);
            }
        });

        button1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/usu/buku/telepon/resource/tentang.png"))); // NOI18N
        button1.setMnemonic('T');
        button1.setText("tentang");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        label5.setText("By Eko Kurniawan Khannedy");

        jScrollPane1.setBorder(null);
        jScrollPane1.setOpaque(false);
        jScrollPane1.setViewport(viewPortTransparan1);

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
        panel1.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(label4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(textPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
                                    .addComponent(textUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addComponent(buttonReset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonMasuk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 285, Short.MAX_VALUE)
                        .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        panel1Layout.setVerticalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(buttonMasuk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(buttonReset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(label2, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE))
                .addContainerGap())
        );

        getContentPane().add(panel1, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-574)/2, (screenSize.height-399)/2, 574, 399);
    }// </editor-fold>//GEN-END:initComponents

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    exit();
}//GEN-LAST:event_formWindowClosing

private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetActionPerformed
    textPassword.setText("");
    textUsername.setText("");
}//GEN-LAST:event_buttonResetActionPerformed

private void buttonMasukActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMasukActionPerformed
    try {
        form.setConnection(getConnection());//GEN-LAST:event_buttonMasukActionPerformed
        textArea.setText("koneksi berhasil");
        setVisible(false);
        form.setVisible(true);
    } catch (SQLException ex) {
        textArea.setText("username dan password yang anda masukkan salah, silahkan gunakan username dan password sesuai dengan profile andministrator MySQL anda");
    }
}

private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
    glassPane.showComponent(about);
}//GEN-LAST:event_button1ActionPerformed

private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed
    glassPane.hideComponent();
}//GEN-LAST:event_aboutActionPerformed

private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
    textUsername.requestFocusInWindow();
}//GEN-LAST:event_formWindowActivated

private void textUsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textUsernameActionPerformed
    textPassword.requestFocusInWindow();
}//GEN-LAST:event_textUsernameActionPerformed

private void textPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textPasswordActionPerformed
    buttonMasukActionPerformed(evt);
}//GEN-LAST:event_textPasswordActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    usu.buku.telepon.widget.About about;
    usu.widget.GlassPane glassPane;
    usu.buku.telepon.widget.TextArea textArea;
    usu.buku.telepon.widget.PasswordBox textPassword;
    usu.buku.telepon.widget.TextBox textUsername;
    // End of variables declaration//GEN-END:variables
}
